package cn.yougou.controller;

import cn.yougou.common.DtoUtil;
import cn.yougou.dto.Dto;
import cn.yougou.service.shopcart.YgCommodityService;
import cn.yougou.vo.YgCommodityOrderByVo;
import cn.yougou.vo.YgCommodityVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * (YgCommodity)表控制层
 *
 * @author 白杰崇
 * @since 2020-12-03 01:32:23
 */
@RestController
@RequestMapping("ygCommodity")
public class YgCommodityController {

    /**
     * 服务对象
     */
    @Reference
    private YgCommodityService ygCommodityService;

    /**
     * 根据三级分类查询所有商品（商品列表展示/销量排序/价格排序）
     * @param orderBy
     */
    @PostMapping("orderBy")
    public Dto orderBy(@RequestBody YgCommodityOrderByVo orderBy){
        Dto dto = null;
        if(orderBy.getCategoryId() != null){
            try{
                orderBy.setDeleted(1);
                List<YgCommodityVo> cateAllTwoList = ygCommodityService.orderBy(orderBy);
                if(!cateAllTwoList.isEmpty()){
                    dto = DtoUtil.returnSuccess("获取成功",cateAllTwoList);
                }
            }catch (Exception e){
                e.printStackTrace();
                dto = DtoUtil.returnFail("系统出错","49999");
            }
        }else{
            return DtoUtil.returnFail("缺少必要参数","50001");
        }
        return dto;
    }

    /**
     * 通过主键查询单条数据
     * @param categoryId
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Dto selectOne(Integer categoryId) {
        Dto dto = null;
        if(categoryId == null){
            return DtoUtil.returnFail("缺少必要参数","50001");
        }else{
            try {
                YgCommodityVo ygCommodityVo = ygCommodityService.queryById(categoryId);
                dto = DtoUtil.returnSuccess("获取成功",ygCommodityVo);
            }catch (Exception e) {
                e.printStackTrace();
                dto = DtoUtil.returnFail("系统出错","49999");
            }
        }
        return dto;
    }



}